home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 193_01 / crypt.doc < prev    next >
Text File  |  1985-11-13  |  17KB  |  334 lines

  1. 12/9/85
  2. INTRODUCTION
  3.  
  4. The "Infinite Key Encryption System" article in issue #94 (August 
  5. 1984)  of Dr.  Dobbs contains an excellent tutorial on Encryption 
  6. Systems.  At  the  time it was published,  I read  it  with  only 
  7. passing  interest.  About six months ago,  I developed a need for 
  8. this type of utility and so begins my story.  The  first thing  I 
  9. did was write a shell cypher program (cypher.c-Listing #1). Since 
  10. I  had already written a generic file copy utility which  allowed 
  11. modifications  during transfer,  it was a simple matter to modify 
  12. the argument passing to include multiple keys and add a  cypher() 
  13. function  call to encrypt the file with a simple exclusive-or'ing 
  14. algorithm (cypher1.c-Listing #2).  While this method did  encrypt 
  15. the  file  and  allowed for easy decryption (using the  same  run 
  16. string),   there   were  definite   detectable  patterns  in  the 
  17. resultant  file.  These patterns,  a function of the key  period, 
  18. were  easily found in areas of repetitive  characters.  (  eg.  a 
  19. string of asterisks or spaces, the hex 1A's at the end of a file, 
  20. etc  )  Another drawback to this scheme is the inability to  pass 
  21. nonprintable  characters in the runstring,  thereby limiting  the 
  22. number  of encryption tokens.  Sooooo it was back to the  drawing 
  23. board.
  24.  
  25. Rereading  the aforementioned article with  renewed  interest,  I 
  26. gained  an  insight  into the methods and  schemes  of  practical 
  27. modern  cyphers.  I don't intend to cover these concepts,  so  if 
  28. you're  interested  or lacking in the basics,  avail yourself  of 
  29. that article, and those in its  bibliography.
  30.  
  31. While the tutorial portion was excellent,  I disagreed with a few 
  32. points on implementation.  First,  there was the code itself  and 
  33. the intimation that assembly language was required for reasonable 
  34. speed.  The  MAC Assembler is only used with CP/M-80 and I wanted 
  35. more portable source,  so I decided to write it in C.  Second,  I 
  36. felt  that a random key  of some prime length could be  generated 
  37. solely from the original key (cypher2.c - Listing  #3).  Although 
  38. some  keys  may  work better than others,  a  means  to  evaluate 
  39. results  can  render this method very  functional.  And  last,  I 
  40. disagreed  with the need for passing information in the encrypted 
  41. file.  It  seemed  unnecessary and cumbersome.  My  goal  was  to 
  42. develop  a  method  that  worked entirely  from  the  run-string.  
  43. Overall  I  must commend the work done by John  Thomas  and  Joan 
  44. Thersites  for  presenting  such a complete  treatment  of  thier 
  45. topic.
  46.  
  47.  
  48. THE ULTIMATE CYPHER
  49.  
  50. The  ultimate cypher is like the ultimate weapon.  No matter  how 
  51. sophisticated,  an  anti-weapon  (anti-cypher) can eventually  be 
  52. developed, IF there is a need. The user must make some judgements 
  53. regarding  needs  and  level  of  protection.  The  2  algorithms 
  54. mentioned  are relatively simple to implement and use.  The  same 
  55. keys  will  encypher  or decypher the file and  key  order  isn't 
  56. important.  The experts (and it becomes quite obvious) point  out 
  57. that  the  strongest  cypher  schemes utilize  a  combination  of 
  58. transposition and substitution.  However,  when  transposition is 
  59. added,  the  order  of decyphering must be the exact  reverse  of 
  60. encyphering.  The  last cypher  module contains an algorithm  for 
  61. transposing  the file tokens along with the random generated  key 
  62. encryption scheme (cypher3.c-Listing #4).  This is a small  price 
  63. to pay for the added security.
  64.  
  65.  
  66. THE NEED FOR TOOLS
  67.  
  68. As  I  progressed in my quest of the   ultimate   cypher/decypher 
  69. algorithm,  I  became  aware of the deficiencies of the  standard 
  70. CP/M utilities at my disposal, so I developed my own.
  71.  
  72. The first tool,  fv.c (file view - Listing #5),  replaced my CP/M 
  73. dump.com.  Dump provides a continual display on screen of the hex 
  74. contents of a file.   Since most encryption is performed on  text 
  75. files, it is most beneficial to include the ascii form along with 
  76. the  hex.  And,  since most algorithms use an exclusive-or as the 
  77. means of encryption it was easy enough to dump two files and  the 
  78. exclusive-ored difference between them.
  79.  
  80. The next tool,  fstat.c (file statistics - Listing #6) calculates 
  81. and  display  the statistical characteristics of the  file.  This 
  82. tool scans the file, counting the occurances of each element, and 
  83. provides a 16 x 16 display of the distribution of characters.  It 
  84. calculates  mean,   median,  mode  and  range  of  the  character 
  85. distribution and displays it's histogram.  As one might  suspect, 
  86. each  file type  has a definite signature.  In fact after limited 
  87. use  of  this  utility the user will be  able  to  recognize  the 
  88. histogram patterns for text, Wordstar, and many other files.
  89.  
  90. While experimenting with various schemes, it becomes obvious that 
  91. the most difficult file to disguise is one that contains a single 
  92. byte for every entry or some sequential scheme.  So the next task 
  93. was  developing a utility,  makef.c (make file - Listing #7),  to 
  94. generate  a known sequential or uni-character file of  some  user 
  95. defined length.
  96.  
  97. Finally the last utility,  sp.c (search pattern - Listing #7),  I 
  98. needed  was  a  search scheme to look  for  repetitive   patterns 
  99. occuring  within  a file and provide some  information  regarding 
  100. location and depth of the repetition. Also included is the option 
  101. to   calculate the delta characteristics of a file to search  for 
  102. repetitive mathematical as well character sequences.
  103.  
  104.  
  105. CYPHER.C - LISTING #1
  106.  
  107. The cypher shell program is provided for use as is,  or for  user 
  108. modification.  It contains the argument passing and file handling 
  109. source  code  needed to copy from an existing file to a new  file 
  110. via  a  16 Kbyte buffer with a cypher function  being  called  to 
  111. encrypt  the  file (If the file is less than 16k the  input  file 
  112. name  may be the same as the output thus destroying the  original 
  113. contents)  A  16K buffer was chosen because it should fit  easily 
  114. with  most  compilers.   This  value  may  be  adjusted  to  meet 
  115. individual  compiler needs.  Any of the 3 algorithms that  follow 
  116. may be included or linked with this shell. Caution is recommended 
  117. to  ensure that the function name is appropriate for  the  method 
  118. you  use.  The  keys  are  passed in the CP/M  command  line  and 
  119. therefore  limited by its length as well as the argument  passing 
  120. capability of the C-compiler.
  121.  
  122.  
  123. CYPHER1.C - LISTING #2
  124.  
  125. This minimal cypher algorithm uses an exclusive-or'ing scheme  to 
  126. encrypt  the file with the keys passed.  If the user employs keys 
  127. of some prime length and performs multiple passes the results can 
  128. be  quite difficult to decypher.  It's quick and easy to use  and 
  129. provides  an excellent means to test the basics of  cryptography. 
  130. Since the keys are limited to only include printable  characters, 
  131. we  don't  take full advantage of the 256 codes available  for  a 
  132. byte.
  133.  
  134. CYPHER2.C - LISTING #3
  135.  
  136. Now  something a little more difficult for the code  breaker,  an 
  137. algorithm  which grew out of the previous listing and generates a 
  138. prime length key for each user key passed. One of 50 prime values 
  139. (betweem  1009  and 1999) is selected as a function  of  the  key 
  140. passed.  The  prime key is then generated using a simple summing-
  141. anding-exclusive oring algorithm and the file is encrypted  using 
  142. this  new  key.  If  two  or more  keys  are  used,  this  method 
  143. guarantees  a  cypher  period in excess of  1,000,000,  which  is 
  144. significantly larger than most text files.
  145.  
  146. The  key generation scheme is based entirely on the original  key 
  147. length  and its contents and I fail to see how this can be worked 
  148. backwards to regain the original, especially if multiple keys are 
  149. used.  Some  keys will generate shorter periods within the  prime 
  150. length,  but  this is easily tested with the  tools  provided.  I 
  151. welcome feedback or suggestions for improving this algorithm.
  152.  
  153.  
  154. CYPHER3.C - LISTING #4
  155.  
  156. Adding  chaos to disorder has probably driven many a code-cracker 
  157. to drink.  And this is just what w